package com.weitu.hiddenDanger.controller;

import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.base.controller.BaseController;
import com.weitu.hiddenDanger.entity.GovernMonReport;
import com.weitu.hiddenDanger.service.HDGovernService;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.user.entity.User;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * 隐患治理Controller
 * Created by fanyuting on 2017/4/14 0014.
 */
@Controller
@RequestMapping("hiddenDanger/hdGovern")
public class HDGovernController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "hiddenDanger/hdGovern/";
    @Autowired
    private HDGovernService hdGovernService;
    @Autowired
    private AccessoryUtilService accessoryUtilService;//附件

    /**
     * 跳转到隐患治理列表页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String toHDGovernListPage(Model model) {

        return MODULE_BATH_PATH + "list";
    }

    /**
     * 查询分页列表
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryHDGovernMonReportList")
    public Object queryHDGovernMonReportList(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        //获取前台参数
        putParametersIntoMap(map);
        Integer totalNum = hdGovernService.queryGovernMonListCount(map);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);
        PageView pageView = pagination.getPageView();//生成pageView分页对象
        map.put("pageView", pageView);
        List<GovernMonReport> list = hdGovernService.queryGovernMonList(map);
        return pagination.flushDataGridData(list);
    }

    /**
     * 跳转到隐患治理月报新增页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String toHDGovernMonReportAddPage(Model model) {
        User loginUser = getSessionUser();
        //model.addAttribute("businessId","123456abcd");
        return MODULE_BATH_PATH + "add";
    }

    /**
     * 隐患月报新增
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public AjaxResult addHDGovernMon(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        getAddBasicFieldMap(map);
        boolean accFlag = false;
        boolean governFlag = false;
        boolean governMonFlag = false;
        //保存月报信息
        governMonFlag = hdGovernService.insertGovernMonReport(map);
        //保存附件登记信息
        if (governMonFlag) {
            accFlag = AccessoryUtil.saveAccerssoryInfo(map.get("id").toString());
        }
        //保存隐患治理信息
        if (governMonFlag) {
            governFlag = hdGovernService.insertGovernInfo(map);
        }
        //
        if (accFlag && governFlag) {
            return success("隐患月报登记成功！");
        }
        return fail("隐患月报登记失败！");
    }

    /**
     * 隐患月报编辑
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public AjaxResult editHDGovernMon(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        getEditBasicFieldMap(map);
        boolean accFlag = false;
        boolean governFlag = false;
        boolean governMonFlag = false;
        //更新月报信息
        governMonFlag = hdGovernService.editGovernMonReport(map);
        //保存附件登记信息
        /*if(governMonFlag){
            accFlag = AccessoryUtil.saveAccerssoryInfo(map.get("id").toString());
        }*/
        //更新隐患治理信息
        if (governMonFlag) {
            governFlag = hdGovernService.editGovernInfo(map);
        }
        //
        if (governFlag) {
            return success("隐患月报登记成功！");
        }
        return fail("隐患月报登记失败！");
    }

    /**
     * 跳转到隐患治理月报编辑页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String toHDGovernMonReportEditPage(Model model, HttpServletRequest request) {
        User loginUser = getSessionUser();
        String businessId = request.getParameter("businessId");//业务Id
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", businessId);
        //查询隐患月报信息
        GovernMonReport govern = hdGovernService.queryGovernMonById(map);
        //model.addAttribute("businessId","123456abcd");
        model.addAttribute("govern", govern);
        return MODULE_BATH_PATH + "edit";
    }

    /**
     * 跳转到隐患治理月报编辑页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String toHDGovernMonReportDetailPage(Model model, HttpServletRequest request) {
        User loginUser = getSessionUser();
        String businessId = request.getParameter("businessId");//业务Id
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", businessId);
        //查询隐患月报信息
        GovernMonReport govern = hdGovernService.queryGovernMonById(map);
        //model.addAttribute("businessId","123456abcd");
        model.addAttribute("govern", govern);
        return MODULE_BATH_PATH + "detail";
    }

    /**
     * 查询隐患治理列表信息
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryHDGovernList")
    public Object queryHDGovernList(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        //获取前台参数
        putParametersIntoMap(map);
        Integer totalNum = hdGovernService.queryGovernListCount(map);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);
        PageView pageView = pagination.getPageView();//生成pageView分页对象
        map.put("pageView", pageView);
        List<Map<String, Object>> list = hdGovernService.queryGovernList(map);
        return pagination.flushDataGridData(list);
    }

    /**
     * 查询隐患治理列表信息（编辑页面）
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryEditPageHDGovernList")
    public Object queryEditPageHDGovernList(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        //获取前台参数
        putParametersIntoMap(map);
        Integer totalNum = hdGovernService.queryEditPageGovernListCount(map);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);
        PageView pageView = pagination.getPageView();//生成pageView分页对象
        map.put("pageView", pageView);
        List<Map<String, Object>> list = hdGovernService.queryEditPageGovernList(map);
        return pagination.flushDataGridData(list);
    }

    /**
     * 删除隐患治理月报信息
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @Transactional
    public AjaxResult deleteGvernMonReport(HttpServletRequest request, HttpServletResponse response) {

        String businessId = request.getParameter("businessId");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", businessId);
        boolean hdFlag = false;
        hdFlag = hdGovernService.deleteGovernMonReport(map);
        if (hdFlag) {
            return success("隐患治理月报信息删除成功！");
        }
        return fail("隐患治理月报信息删除失败！");
    }
}
